package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.merchant.StageDelete;
import com.sand.dto.worker.SelectProject;
import com.sand.entity.conclude.contract;
import com.sand.entity.work.Project;
import com.sand.entity.work.Project_Worker;
import com.sand.feign.InventoryFeign;
import com.sand.feign.MerchantFegin;
import com.sand.mapper.ProjectMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.ProjectService;
import com.sand.service.Project_workerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private MerchantFegin merchantFegin;

    @Autowired
    private Project_workerService projectWorkerService;

    @Autowired
    private InventoryFeign inventoryFeign;

    /**
     * 创建工程
     *
     * @param project
     */
    @Override
    @Transactional
    public Integer add(Project project) {

        //参数判断
        pan(project);

        //添加
        save(project);

        //删除合同预购
        merchantFegin.down(new StageDelete(null,project.getContractid()));

        //删除库存
        try {
            inventoryFeign.subtract(project.getContractid());
        } catch (Exception e) {
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"库存不够无法创建工程");
        }

        return 1;
    }

    /**
     * 条件查看该用户的所有工程
     *
     * @param selectProject
     */
    @Override
    public List<Project> getall(SelectProject selectProject) {

        Integer ownerid = selectProject.getOwnerid();

        Integer principal = selectProject.getPrincipal();

        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();

        if (ownerid == null && principal == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"为传入要查询的id");
        }

        if (ownerid != null && principal != null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"不能同时查询多个id");
        }

        if (selectProject.getComplete() != null){
            wrapper.eq(Project::getComplete,selectProject.getComplete());
        }

        if (ownerid != null){

            wrapper.eq(Project::getOwnerid,ownerid);

            return list(wrapper);
        }

        wrapper.eq(Project::getPrincipal,principal);

        return list(wrapper);
    }


    /**
     * 查看该工程下的工人
     *
     * @param projectid
     */
    @Override
    public List<Project_Worker> getw(Integer projectid){

        if (projectid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程id");
        }

        return projectWorkerService.list(new LambdaQueryWrapper<Project_Worker>().eq(Project_Worker::getProjectid, projectid));
    }

    /**
     * 工程完工
     *
     * @param projectid
     */
    @Override
    @Transactional
    public Integer ok(Integer projectid) {

        if (projectid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程id");
        }

        //判断工程是否存在
        Project project = getById(projectid);
        if (project == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"工程不存在");
        }

        //修改工程
        project.setComplete(1);
        project.setOvertime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        updateById(project);

        //调用完成合同
        merchantFegin.ok(project.getContractid());

        return 1;
    }

    //创建工程的参数校验
    private void pan(Project project) {

        if (project.getName() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程名称");
        }

        if (project.getOwnerid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程的业主");
        }

        if (project.getPrincipal() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程的负责人");
        }

        if (project.getContractid() == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程的合同");
        }

        //判断传入的合同id
        contract data = merchantFegin.get(project.getContractid()).getData();

        if (data == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"添加的合同id不存在");
        }

        //判断业主和负责人要是合同中的甲已方
        Integer ownerid = data.getOwnerid();

        Integer merchantid = data.getMerchantid();

        if (!Objects.equals(project.getOwnerid(), ownerid) || !Objects.equals(project.getPrincipal(), merchantid)){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"添加的人员与合同中的人员不一致");
        }

        //格式校验和初始化
        chu(project);
    }

    //创建工程的初始化
    private void chu(Project project) {

        if (project.getName().length() > 20){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"工程名称长度不能大于20");
        }

        //初始化开始时间
        if (project.getBegintime() == null){
            project.setBegintime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }

        //默认添加时不添加结束时间
        project.setOvertime(null);

        //默认工程未完成
        project.setComplete(0);

    }


}
